package test.microservice.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import test.microservice.dao.UserRepository;
import test.microservice.entity.DockerUser;

import java.util.Collection;

@RestController
public class UserController {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
    
    @Autowired
    private UserRepository userRepository;
    
    @GetMapping("/{id}")
    public DockerUser findById(@PathVariable Long id) {
//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        if (principal instanceof UserDetails) {
//            UserDetails userDetails = (UserDetails) principal;
//            Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
//            for (GrantedAuthority authority : authorities) {
//                LOGGER.info("current user:{}, current role:{}", userDetails.getUsername(), authority.getAuthority());
//            }
//        } else {
//            LOGGER.info("illegal UserDetails type ...");
//        }
        DockerUser dockerUser = this.userRepository.findOne(id);
        return dockerUser;
    }
}
